import { saveLink } from "@/api/link.api";
import { Link } from "@/types/link";
import { ChangeEvent, useState } from "react";


export default function useSaveLink (): {
  linkForm: Link;
  save: () => void;
  handleLinkForm:(e:ChangeEvent<HTMLInputElement>,key:string)=>void
} {

  const [linkForm,setLinkForm] = useState<Link>({
    id: "",
    name: "",
    logo: "",
    description: "",
    address: "",
    status: "2",
  })
  const save = async () => {
    if (!(linkForm.name && linkForm.address && linkForm.logo)) return;
    const res = await saveLink(linkForm);
    if (res.code === 200) {
      setLinkForm({
          ...linkForm,
          address:'',
          logo:'',
          description:'',
          name:''
      })
    }
  };
  const handleLinkForm = (e:ChangeEvent<HTMLInputElement>,key:string)=>{
    setLinkForm({
      ...linkForm,
      [key]:e.target.value
    })
  }
  return {
    linkForm,
    save,
    handleLinkForm
  };
}
